import React from 'react';
import { Card, Tabs, Icon} from 'antd';

import './ui.scss';

const TabPane = Tabs.TabPane;

export default class Notice extends React.Component {
  constructor(props) {
    super(props);
    const panes = [{
      title: 'Tab 1', content: 'Content of Tab 1', key: '1' 
    },{
      title: 'Tab 2', content: 'Content of Tab 2', key: '2' 
    },{
      title: 'Tab 3', content: 'Content of Tab 3', key: '3', closable: false 
    }];
    this.state = {
      panes,
      activeKey: panes[0].key
    };
    this.newTabIndex = 0;
  }
  tabsCallback(key) {
    console.log(key);
  }

  onTabsEdit(targetKey, action) {
    this[action](targetKey);
  }

  add(){
    const panes = this.state.panes;
    const activeKey = `newTab${this.newTabIndex++}`;
    panes.push({ title: 'New Tab', content: 'Content of new Tab', key: activeKey });
    this.setState({ panes, activeKey });
  }

  remove(targetKey){
    let activeKey = this.state.activeKey;
    let lastIndex;
    this.state.panes.forEach((pane, i) => {
      if (pane.key === targetKey) {
        lastIndex = i - 1;
      }
    });
    const panes = this.state.panes.filter(pane => pane.key !== targetKey);
    if (lastIndex >= 0 && activeKey === targetKey) {
      activeKey = panes[lastIndex].key;
    }
    this.setState({ panes, activeKey });
  }

  render() {
    return (
      <div className="tabs/disabled">
        <Card title="tabs/disabled">
          <Tabs defaultActiveKey="1" onChange={(key) => this.tabsCallback(key)}>
            <TabPane tab="Tab 1" key="1">Content of Tab Pane 1</TabPane>
            <TabPane tab="Tab 2" key="2" disabled>Content of Tab Pane 2</TabPane>
            <TabPane tab="Tab 3" key="3">Content of Tab Pane 3</TabPane>
          </Tabs>
        </Card>

        <Card title="contoms tabs icon">
          <Tabs defaultActiveKey="1" onChange={(key) => this.tabsCallback(key)}>
            <TabPane tab={<span><Icon type="apple" size="small"/> tab 1</span>} key="1">this is tabs of apple!</TabPane>
            <TabPane tab={<span><Icon type="delete" size="primary"/> tab 2</span>} key="2">this is tabs of delete!</TabPane>
            <TabPane disabled tab={<span><Icon type="plus" size="large"/> tab 2</span>} key="3">this is tabs of plus!</TabPane>
          </Tabs>
        </Card>

        <Card title="contoms tabs add and delete">
          <Tabs
            type="editable-card"
            onEdit={(targetKey, action) => this.onTabsEdit(targetKey, action)}
          >
            {this.state.panes.map(pane => <TabPane tab={pane.title} key={pane.key} closable={pane.closable}>{pane.content}</TabPane>)}
          </Tabs>
        </Card>
      </div>
    );
  }

}